<?php

namespace Goods\Controller;

use Common\Controller\AdminbaseController;

class AdminGoodsController extends AdminbaseController {
	
	protected $goods_model;
		
	function _initialize() {
		parent::_initialize();
		$this->goods_model = D("Goods/Goods");
		$this->category_model = D("Goods/GoodsCategory");
		
	}
	
	// 后台商品分类列表
    public function index(){
    	$course = M('course')->field('id,cs_name')->order("listorder asc")->select();
		
		$where = "del = '0'";
		if(isset($_POST['courseid'])){
			$cid = $_POST['courseid'];
			$state = $_POST['state'];
			$keyword = $_POST['keyword'];
			if($cid && $cid != 0){
				$where .= " and courseid = '$cid'";
			}
			if($state && $state != ''){
				$where .= " and state = '$state'";
			}
			if($keyword){
				$where .= " and (`name` like '%$keyword%' or content like '%$keyword%') ";
			}
		}
		
		
    	$count=M('goods_j')->where($where)->count();
		$page = $this->page($count, 20);	
		$data = M('goods_j')->where($where)->order("addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		
		$this->assign('course',$course);
		$this->assign('data',$data);
		$this->_getTree();
		$this->assign("Page", $page->show('Admin'));
		$this->assign('count',$count);
		$this->display();
	}
	
	
	// 后台商品分类列表
	public function ajaxgoodslist(){
		$where['is_delete'] = array('neq',3);
		
		$keyword=trim(I('keyword'));
        if($keyword)
        {
			 $where['goods_name']=array('like',"%$keyword%");			 	 
        }
		$cat_id = I('cat_id');
		if($cat_id)
		{
			$where['cat_id']=$cat_id;
		}
		$is_on_sale = I('is_on_sale');
		if($is_on_sale !== ''){
			$where['is_on_sale']=$is_on_sale;
		}
		$intro = I('intro');
		if($intro !== ''){
			$where[$intro] = 1;
		}
		
		$count=$this->goods_model->where($where)->count();		
		$page = $this->ajaxpage($count,10);								
		$order_str = "a.{$_POST['orderby1']} {$_POST['orderby2']},a.last_update DESC";
		$this->goods_model->field('a.*,b.name');
		$this->goods_model
		->alias('a')
		->join('__GOODS_CATEGORY__ b ON a.cat_id = b.id')
		->where($where)
		->limit($page->firstRow , $page->listRows)			
		->order($order_str);		
		
		$goodslist = $this->goods_model->select();
		
		//print_r($goodslist);die;
		$this->assign('goodslist',$goodslist);
		$this->assign('page',$page->show('Admin'));
		$this->display();
	}
	
	public function _getTree(){
		$tree = new \Tree();
	 	$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
	 	$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
	 	$category = $this->category_model->order(array("parent_id_path"=>"asc"))->select();
	 	
	 	$new_category=array();
	 	foreach ($category as $r) {
	 		$r['id']=$r['id'];
	 		$r['parentid']=$r['parent_id'];
	 		$r['selected']= (!empty($parentid) && $r['id']==$parentid)? "selected":"";
	 		$new_category[] = $r;
	 	}
	 	$tree->init($new_category);
	 	$tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
	 	$tree=$tree->get_tree(0,$tree_tpl);
	 	
	 	$this->assign("category_tree",$tree);
	}
	
	public function add(){
		//商品分类
		$tree = new \Tree();
	 	$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
	 	$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
	 	$category = M('course_type')->order(array("path"=>"asc"))->select();
	 	$new_category=array();
	 	foreach ($category as $r) {
	 		$r['id']=$r['term_id'];
	 		$r['parentid']=$r['parent'];
	 		$r['selected']= (!empty($parentid) && $r['id']==$parentid)? "selected":"";
	 		$new_category[] = $r;
	 	}
	 	$tree->init($new_category);
	 	$tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
	 	$tree=$tree->get_tree(0,$tree_tpl);
		
		if(isset($_POST['cid'])){
			$cid = $_POST['cid'];
			$course = M('course')->field("id,cs_name")->where("ty_id = '$cid' and cs_state = '1'")->order("listorder asc")->select();
		}
		
		//商品类型
		$goods_type = M('GoodsType')->select();
	 	
	 	$this->assign("course",$course);
	 	$this->assign("category_tree",$tree);
		$this->assign("goods_type",$goods_type);
		$this->display();
	}
	
	//ajax获取规格
	public function ajaxGetSpec(){
		$id = I("get.type_id",0,'intval');
		$goods_id = I("get.id",0,'intval');
		$spec = M('GoodsSpec')->where(array("type_id"=>$id))->order(array("order"=>"asc"))->select();
		
		foreach($spec as $key=>$v){
			$spec[$key]['items'] = M('GoodsSpecItem')->where(array("spec_id"=>$v['id']))->getField('id,item');
		}
		
		//获取规格id		
		$items_id = M('GoodsSku')->where(array("goods_id"=>$goods_id))->getField("GROUP_CONCAT(`item_path` SEPARATOR '-') AS items_id");		
        $items_ids = explode('-', $items_id); 		
		
		$this->assign('spec',$spec);
		$this->assign('items_ids',$items_ids);
		$this->assign("goods_id",$goods_id);
		$this->display("ajaxspec");
	}
	
	/**
     * 动态获取商品规格输入框 根据不同的数据返回不同的输入框
     */    
    public function ajaxGetSpecInput(){     
         $GoodsLogic = new \Goods\Logic\GoodsLogic();
		
         $goods_id = I("goods_id",0,'intval');
         $str = $GoodsLogic->getSpecInput($goods_id ,$_POST['spec_arr']);
         exit($str);   
    }
	
	public function add_post(){
		if(IS_POST){
				$_POST['addtime'] = time();
				$add = M('goods_j')->add($_POST);
				if($add) {
					echo 1;
				} else {
					echo 2;
				}			
		}
	}
	
	public function edit(){
		
		$id = I("get.id",0,'intval');
		$data=M('goods_j')->where(array("id" => $id))->find();
		$parentid = $data['cid'];
		$tree = new \Tree();
	 	$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
	 	$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
	 	$category = M('course_type')->order(array("path"=>"asc"))->select();
	 	$cat_id = $data['cat_id'];
	 	$new_category=array();
	 	foreach ($category as $r) {
	 		$r['id']=$r['term_id'];
	 		$r['parentid']=$r['parent'];
	 		$r['selected']= (!empty($parentid) && $r['id']==$parentid)? "selected":"";
	 		$new_category[] = $r;
	 	}
		
		$classid = $data['cid'];
		$course = M('course')->field("id,cs_name")->where("ty_id = '$classid'")->order("listorder asc")->select();
		if(isset($_POST['cid'])){
			$cid = $_POST['cid'];
			$course = M('course')->field("id,cs_name")->where("ty_id = '$cid'")->order("listorder asc")->select();
		}
		
	 	$tree->init($new_category);
	 	$tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
	 	$tree=$tree->get_tree(0,$tree_tpl);
		
		
	 	$this->assign("course",$course);	 					
		$this->assign("category_tree",$tree);
		$this->assign("data",$data);
		$this->assign("photo",json_decode($data['photo'],true));
		$this->assign("goods_type",$goods_type);		
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$id = $_POST['jid'];
			$save = M('goods_j')->where("id = '$id'")->save($_POST);
			if ($save) {
				echo 1;
			} else {
				echo 2;
			}
		}
	}
	public function goodup(){
		$id = $_POST['id'];
		$data['state'] = 0;
		$save = M('goods_j')->where("id = '$id'")->save($data);
		if($save){
			echo 1;
		}else{
			echo 2;
		}
	}
	public function gooddown(){
		$id = $_POST['id'];
		$data['state'] = 1;
		$save = M('goods_j')->where("id = '$id'")->save($data);
		if($save){
			echo 1;
		}else{
			echo 2;
		}
	}
	//放入回收站
	public function delete(){
		$id = $_GET['id'];
		$result = M('goods_j')->where("id = $id")->save(array('del'=>1));
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	
	public function delete_all(){
		if(isset($_POST['ids'])){
			$ids = I('post.ids/a');
			
			if ($this->goods_model->where(array('goods_id'=>array('in',$ids)))->save(array('is_delete'=>3))!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}
	
	//回收站列表
	public function recyle(){
		
		$this->display();
	}
	
	public function ajaxrecyle(){
		$where['is_delete'] = '3';
		$keyword=trim(I('keyword'));
        if($keyword)
        {
			 $where['goods_name']=array('like',"%$keyword%");			 	 
        }
		$count=$this->goods_model->where($where)->count();		
		$page = $this->ajaxpage($count,10);
		$this->goods_model
		->where($where)
		->limit($page->firstRow , $page->listRows)
		->order("goods_id DESC");		
		
		$goodslist = $this->goods_model->select();
		
		$this->assign('goodslist',$goodslist);
		$this->assign('page',$page->show('Admin'));
		
		$this->display();
	}
	
	//还原
	public function restore(){
		$id = $_GET['id'];
		$result = $this->goods_model->where("goods_id = $id")->save(array('is_delete'=>0));
		if($result){
			$this->success('还原成功');
		}else{
			$this->error('还原失败');
		}
	}
	
	//删除
	public function clean(){
		$id = $_GET['id'];
		$result = $this->goods_model->where(array("goods_id" => $id,"is_delete"=>3))->delete();
		M('GoodsSku')->where(array("goods_id" => $id))->delete();//删除产品sku
		M('GoodsSpecImg')->where(array("goods_id" => $id))->delete();//删除规格图片
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	
	public function clean_all(){
		if(isset($_POST['ids'])){
			$ids = I('post.ids/a');
			M('GoodsSku')->where(array('goods_id'=>array('in',$ids)))->delete();//删除产品sku
			M('GoodsSpecImg')->where(array('goods_id'=>array('in',$ids)))->delete();//删除规格图片
			if ($this->goods_model->where(array('goods_id'=>array('in',$ids),"is_delete"=>3))->delete()!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}
	
	/**
     * ajax 修改指定表数据字段  一般修改状态 比如 是否推荐 是否开启 等 图标切换的
     * table,id_name,id_value,field,value
     */
    public function changeTableVal(){  	
            $table = I('table'); // 表名
            $id_name = I('id_name'); // 表主键id名
            $id_value = I('id_value'); // 表主键id值
            $field  = I('field'); // 修改哪个字段
            $value  = I('value'); // 修改字段值                        
            M($table)->where("$id_name = $id_value")->save(array($field=>$value)); // 根据条件保存修改的数据
    }
	
}